package jt.classic.app.gdzc.storage;

import org.limp.basework.MachiningException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.Table;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.DateTrimmer;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


/**
 * 资产入(出)库数据加工类
 * <p>
 *   用于完成资产入库时更新资产基本信息表的库存字段
 * </p>
 * @author mido
 *
 */
public class StorageMachining implements SimpleBeanMachining {
    public SimpleBean machining(SimpleBean bean, Table table,
        HttpServletRequest request, Connection conn, int type)
        throws MachiningException {
        // TODO Auto-generated method stub
    	/*资产入库*/
        if (table.getName().equals("APP_GDZC_KUCUN") && (type == 0)) {
            String gzid = (String) bean.getResource().get("GZID");
            String ckid = (String) bean.getResource().get("CKID");

            SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_GDZC");
            sbt.getResource().put("GZID", gzid);
            sbt.getResource().put("CKID", ckid);
            sbt.getResource().put("CKSTATUS", "1");
            sbt.getResource().put("CKDATE", DateTrimmer.getYMDHMS());

            BaseworkUtil util = new BaseworkUtil();

            try {
                util.manualUpdate1(sbt, conn);
            } catch (Exception ex) {
                throw new MachiningException("Storage machining error.", ex);
            }
        }
        /*资产领用出库*/
        else if(table.getName().equals("APP_GDZC_LY") && (type == 1)){
        	 List list = (List) bean.getResource().get("APP_GDZC_LY");

             if ((list != null) && !list.isEmpty()) {
                 BaseworkUtil util = new BaseworkUtil();

                 for (int i = 0; i < list.size(); i++) {
                     Map temp = (Map) list.get(i);

                     String gzid = (String) temp.get("GZID");
                     String lystatus = (String) temp.get("LYSTATUS");

                     SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_GDZC");

                     /*资产出库*/
                     if (lystatus.equals("2")) {
                         sbt.getResource().put("GZID", gzid);
                         /*更新资产库存状态*/
                         sbt.getResource().put("CKSTATUS", "-1");

                         try {
                             util.manualUpdate1(sbt, conn);
                         } catch (Exception ex) {
                             throw new MachiningException("Consuming machining error.",
                                 ex);
                         }
                     }
                 }
             }
        }

        return null;
    }
}
